<?php

namespace app\proxy\controller\product;

use app\proxy\controller\Controller;
use app\proxy\model\product\ProductLabel as LabelModel;
use app\proxy\model\auth\User as ProxyUser;

/**
 * 标签管理控制器
 */
class ProductLabel extends Controller
{
    /**
     * 获取列表
     */
    public function index()
    {
        // 列表
        $model   = new LabelModel;
        $regions = $this->getRegionIds();
        $list    = $model->getList($this->request->param(), 0, $this->getProxyId());
        // 数量
        $category_count = [];
        $category_count = [
            'using'   => $model->getProxyTotal(['audit_status' => 1], 0, $this->getProxyId()),
            'audit'   => $model->getProxyTotal(['audit_status' => 0], 0, $this->getProxyId()),
            'no_pass' => $model->getProxyTotal(['audit_status' => 2], 0, $this->getProxyId()),
        ];
        return $this->renderSuccess('', compact('list', 'category_count'));
    }

    /**
     * 添加
     */
    public function add()
    {
        $nosh = ProxyUser::checkauth($this->getProxyId(),'label');
        $model = new LabelModel;
        // 新增记录
        if ($model->add(array_merge($this->postData(), ['audit_status' => $nosh?1:0]), $this->getProxyId())) {
            return $this->renderSuccess('添加成功');
        }
        return $this->renderError($model->getError() ?: '添加失败');
    }

    /**
     * 编辑
     */
    public function edit($product_label_id)
    {
        $nosh = ProxyUser::checkauth($this->getProxyId(),'label');
        // 详情
        $model = LabelModel::detail($product_label_id);
        if ($model->isEmpty() || $model['proxy_id'] != $this->getProxyId()) {
            return $this->renderError('暂无权限操作');
        }
        // 更新记录
        if ($model->edit(array_merge($this->postData(), ['audit_status' => $nosh?1:0]), $this->getProxyId())) {
            return $this->renderSuccess('更新成功');
        }
        return $this->renderError($model->getError() ?: '更新失败');
    }

    /**
     * 删除
     */
    public function delete($product_label_id)
    {
        $model = LabelModel::detail($product_label_id);
        if ($model->isEmpty() || $model['proxy_id'] != $this->getProxyId()) {
            return $this->renderError('暂无权限操作');
        }
        if (!$model->remove()) {
            return $this->renderError('该标签使用中，删除失败');
        }
        return $this->renderSuccess('删除成功');
    }
}